<?php
require_once 'playerseasonmatches.php';

class PlayerLeagueMatches {
  const THISSEASON_XPATH = "./this[1]";
  const LASTSEASON_XPATH = "./last[1]";
  const CARRIER_XPATH = "./career[1]";

  private $thisSeason;
  private $lastSeason;
  private $carrier;
  private $db;

  public function __construct($database) {
    $this->db = $database;

    $this->thisSeason = new PlayerSeasonMatches($this->db);
    $this->lastSeason = new PlayerSeasonMatches($this->db);
    $this->carrier = new PlayerSeasonMatches($this->db);
  }

  public function loadFromNode(DOMElement $docNode) {
    $doc = $docNode->ownerDocument;

    $xPath = new DOMXPath($doc);

    $qResult = $xPath->query(PlayerLeagueMatches::THISSEASON_XPATH,$docNode);
    $this->thisSeason->loadFromNode($qResult->item(0));

    $qResult = $xPath->query(PlayerLeagueMatches::LASTSEASON_XPATH,$docNode);
    $this->lastSeason->loadFromNode($qResult->item(0));

    $qResult = $xPath->query(PlayerLeagueMatches::CARRIER_XPATH,$docNode);
    $this->carrier->loadFromNode($qResult->item(0));
  }

  public function toXHTML() {
    $xhtml = $this->thisSeason->toXHTML();
    $xhtml .= $this->lastSeason->toXHTML();
    $xhtml .= $this->carrier->toXHTML();

    return $xhtml;
  }

  public function insertNewToDB($matchesId) {
    $query = $this->db->query('SHOW TABLE STATUS LIKE \'playerleaguematches\';');
    if (!$query) {
      log_message('error', 'Database is inconsistance!');
    }

    $leagueMatchesId = $query->row()->Auto_increment;

    $thisSeasonId = $this->thisSeason->insertNewToDB($leagueMatchesId);
    $lastSeasonId = $this->lastSeason->insertNewToDB($leagueMatchesId);
    $carrierId = $this->carrier->insertNewToDB($leagueMatchesId);

    $this->db->set('id',$leagueMatchesId);
    $this->db->set('matchesId',$matchesId);
    $this->db->set('thisSeason',$thisSeasonId);
    $this->db->set('lastSeason',$lastSeasonId);
    $this->db->set('carrier',$carrierId);

    $query = $this->db->insert('playerleaguematches');

    return $this->db->insert_id();
  }

  public function amendOldInDB($id) {
    $this->db->where('id',$id);
    $query = $this->db->get('playerleaguematches');

    if (!$query) {
      log_message('error','Error during playerleaguematches amendment!');
      return FALSE;
    }
    $tLeagueMatches = $query->row();

    $thisSeason = $this->thisSeason->amendOldInDB($tLeagueMatches->thisSeason);
    $lastSeason = $this->lastSeason->amendOldInDB($tLeagueMatches->lastSeason);
    $carrier = $this->carrier->amendOldInDB($tLeagueMatches->carrier);

    $this->db->set('thisSeason',$thisSeason);
    $this->db->set('lastSeason',$lastSeason);
    $this->db->set('carrier',$carrier);
    $this->db->where('id',$id);
    $query = $this->db->update('playerleaguematches');

    if (!$query) {
      log_message('error', 'Error during playerleaguematches amendment!');
    }

    return $id;
  }

  public function loadFromDB(ADOConnection $conn, $leagueMatchesId) {
    $stmt = $conn->Prepare("SELECT * FROM `playerleaguematches` WHERE `leagueMatchesId` = ?");
    $rs=$conn->Execute($stmt,array($leagueMatchesId));

    if (!$rs) {
      $logger = Logger::getInstance();
      $logger->log($conn->ErrorMsg(), Logger::ERR);
    }

    if ($rs->RecordCount()==0) {
      $logger = Logger::getInstance();
      $logger->log("PlayerLeagueMatches not found in the database!", Logger::ERR);
    } else {
      $result = $rs->FetchRow();

      $this->thisSeason->loadFromDB($conn, $result['thisSeason']);
      $this->lastSeason->loadFromDB($conn, $result['lastSeason']);
      $this->carrier->loadFromDB($conn, $result['carrier']);
    }
  }
}
?>
